package com.sls.web.manager.action.game;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.common.FilterStringSpaces;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.OhFly;
import com.sls.web.manager.game.pojo.OhFlyUserInfo;
import com.sls.web.manager.game.pojo.SyncInfo;
import com.sls.web.manager.game.querybean.OhFlyQuery;
import com.sls.web.manager.game.service.OhFlyService;
import com.sls.web.manager.page.Pagination;

public class OhflyGoodsSyncAction extends SuperAction {

	private static final long serialVersionUID = 1L;

	@Resource(name = "ohFlyService")
	private OhFlyService ohFlyService;
	private OhFly ohFly;
	private OhFlyUserInfo ohFlyUserInfo;
	private OhFlyQuery ohFlyQuery;
	private Pagination<OhFly> page;
	private List<OhFlyUserInfo> users;

	/**
	 * 列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		try {
			if (ohFlyQuery == null) {
				ohFlyQuery = new OhFlyQuery();
				ohFlyQuery.setPageSize(Constants.INIT_PAGESIZE);
				ohFlyQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
			}
			String goodsId = ohFlyQuery.getGoodsId();
			String goodsName = ohFlyQuery.getGoodsName();
			goodsId = FilterStringSpaces.checkStringHasSpace(goodsId);
			goodsName = FilterStringSpaces.checkStringHasSpace(goodsName);
			ohFlyQuery.setGoodsId(goodsId);
			ohFlyQuery.setGoodsName(goodsName);
			page = ohFlyService.queryOhFly(ohFlyQuery, getCurrentOperator());
			users = ohFlyService.queryOhFlyUserInfo(getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return LIST;
	}

	/**
	 * 具体商品信息同步
	 * 
	 * @return
	 * @throws Exception
	 */
	public String sync() throws Exception {
		try {
			ohFlyUserInfo.setVersion(Constants.OH_FLY_VERSION);
			List<OhFlyUserInfo> userList = ohFlyService
					.queryOhFlyUserInfo(getCurrentOperator());
			String username = ohFlyUserInfo.getUserid();
			if(StringUtils.isNotEmpty(username)){
				username = username.trim();
			}
			if (userList != null) {
				for (OhFlyUserInfo user : userList) {
					String userid = user.getUserid();
					if (StringUtils.isNotEmpty(userid)) {
						userid = userid.trim();
					}
					if (username.equals(userid)) {
						String userpws = user.getUserpws();
						if (StringUtils.isNotEmpty(userpws)) {
							userpws = userpws.trim();
						}
						ohFlyUserInfo.setUserpws(userpws);
						break;
					}
				}

			}
			SyncInfo syncInfo = ohFlyService.detailGoodsInfoSync(ohFlyUserInfo,
					getCurrentOperator());
			if (syncInfo != null) {
				String retCode = syncInfo.getRetcode();
				if (StringUtils.isNotEmpty(retCode) && !"1".equals(retCode)) {
					flag = false;
					addActionMessage(syncInfo.getErr_msg());
				} else {
					int resNum = syncInfo.getResNum();
					addActionMessage(String.valueOf(resNum));
				}
			}
			redirectionUrl = "ohfly_goods_sync!list.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return "sync_success";
	}

	public OhFlyService getOhFlyService() {
		return ohFlyService;
	}

	public void setOhFlyService(OhFlyService ohFlyService) {
		this.ohFlyService = ohFlyService;
	}

	public OhFlyUserInfo getOhFlyUserInfo() {
		return ohFlyUserInfo;
	}

	public void setOhFlyUserInfo(OhFlyUserInfo ohFlyUserInfo) {
		this.ohFlyUserInfo = ohFlyUserInfo;
	}

	public OhFly getOhFly() {
		return ohFly;
	}

	public void setOhFly(OhFly ohFly) {
		this.ohFly = ohFly;
	}

	public OhFlyQuery getOhFlyQuery() {
		return ohFlyQuery;
	}

	public void setOhFlyQuery(OhFlyQuery ohFlyQuery) {
		this.ohFlyQuery = ohFlyQuery;
	}

	public Pagination<OhFly> getPage() {
		return page;
	}

	public void setPage(Pagination<OhFly> page) {
		this.page = page;
	}

	public List<OhFlyUserInfo> getUsers() {
		return users;
	}

	public void setUsers(List<OhFlyUserInfo> users) {
		this.users = users;
	}
}
